<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Api\V1\Resources\VipPurchaseResource;
use App\Services\BuyVip\BuyVip;
use App\Services\BuyVip\BuyVipRequest;
use Illuminate\Support\Facades\Cache;

class VipPurchasesController extends Controller
{
	public function store()
	{	

		$lock = Cache::lock('buyVip', 1);
		try {

			if (! $lock->get()) fail('太快了，请再试一次');

    		$request = app(BuyVipRequest::class);

    		$user = $request->user();

		  	$record = app(BuyVip::class, [
			    'user' => $user,
			    'vip_category_id' => $request->vip_category_id,
			    'vip_purchase_id' => null,
			    'address' => $request->getAddress(),
		   		'items' => $request->items
		    ])->buy();

	    } finally {
    		$lock->release();
	    }

		return new VipPurchaseResource($record);
	}


	public function take()
	{	

		$lock = Cache::lock('take', 1);
		try {

			if (! $lock->get()) fail('太快了，请再试一次');

		  	$user = request()->user();
			$vipWelfare = $user->userVipWelfare();

	    } finally {
    		$lock->release();
	    }

		 return response(null, 204);
	}
}
